// 数据验证,自定义验证
const mongoose = require('mongoose');

async function main(){
    await mongoose.connect('mongodb://localhost:27017/local')
    console.log('dataBase connect success')


    let validateSchema = new mongoose.Schema({
        name:{
            type:String,
            required:true,
            validate:(val)=>{
                if(val.length < 0 || val.length>20){
                    return false;
                }
            }
        },
        count:{
            type:Number,
            max:100,
            min:10,
            required:true
        }
    })

    let Book = mongoose.model('book',validateSchema)

    let book = new Book({
        name:'你好',
        count:1000
    })

    book.save()
}
main()